#!/bin/bash
clear

TEXT='\033[30;107;2m'
INFO='\033[97;44m'
WARNING='\033[97;43m'
ERROR='\033[97;41m'
SUCCESS='\033[97;42m'
GREEN='\033[92m'
YELLOW='\033[93m'
RESET='\033[0m'

if [ "${EUID}" -ne 0 ]; then
  echo -e "${ERROR} RALAT ${RESET}${TEXT} Anda tiada kebenaran untuk menjalankan skrip ini! ${RESET}"
  exit 1
fi

function installPkg {
  read -r -p "Teruskan dengan pemasangan semula? [Y/n] " _yesno
  if [[ "$_yesno" =~ ^[Yy]$ ]]; then
    if [[ ! -d /etc/openvpn ]]; then
      wget -q -O - https://raw.githubusercontent.com/cybertize/debian/buster/packages/ovpn.sh | bash
    fi
    systemctl stop openvpn
    apt-get -y -qq --purge remove openvpn &>/dev/null
    apt-get -y -qq autoremove &>/dev/null
    apt-get -qq autoclean &>/dev/null
    clear && sleep 3
    wget -q -O - https://raw.githubusercontent.com/cybertize/debian/buster/packages/ovpn.sh | bash
  else
    echo -e "${ERROR} INFO ${RESET}${TEXT} Membatalkan pemasangan semula ${RESET}"
    exit 1
  fi
}

function uninstallPkg {
  read -r -p "Adakah anda pasti mahu menyahpasang openvpn? [Y/n] " _yesno
  if [[ "$_yesno" =~ ^[Yy]$ ]]; then
    apt-get -y -qq --purge remove openvpn &>/dev/null
    apt-get -y -qq autoremove &>/dev/null
    apt-get -qq autoclean &>/dev/null

    echo -e "${SUCCESS} INFO ${RESET}${TEXT} Bersihkan & Keluarkan pakej openvpn ${RESET}"
    exit 0
  else
    echo -e "${ERROR} INFO ${RESET}${TEXT} Batalkan penyahpasangan pakej ${RESET}"
    exit 1
  fi
}

function configurePkg {
  server-tcp() {
    local tcp_port=$(cat /etc/openvpn/server-tcp.conf | grep -w 'port' | cut -d ' ' -f 2)

    until [[ -n "$input_tcp_port" && "$input_tcp_port" =~ ^[0-9]+$ ]]; do
      read -p "Enter new port: " input_tcp_port
      sed -i "s|$tcp_port|$input_tcp_port|g" /etc/openvpn/server-tcp.conf
      systemctl restart openvpn@server-tcp
    done
  }
  server-udp() {
    local udp_port=$(cat /etc/openvpn/server-udp.conf | grep -w 'port' | cut -d ' ' -f 2)

    until [[ -n "$input_udp_port" && "$input_udp_port" =~ ^[0-9]+$ ]]; do
      read -p "Enter new port: " input_udp_port
      systemctl stop openvpn@server-udp
      sed -i "s|$udp_port|$input_udp_port|g" /etc/openvpn/server-udp.conf
      systemctl start openvpn@server-udp
    done
  }
  server-tls() {
    local tls_port=$(cat /etc/openvpn/server-tls.conf | grep -w 'port' | cut -d ' ' -f 2)

    until [[ -n "$input_tls_port" && "$input_tls_port" =~ ^[0-9]+$ ]]; do
      read -p "Enter new port: " input_tls_port
      systemctl stop openvpn@server-tls
      systemctl stop stunnel4

      sed -i "s|$tls_port|$input_tls_port|g" /etc/openvpn/server-tls.conf
      sed -i "s|connect = 127.0.0.1:$tls_port|connect = 127.0.0.1:$input_tls_port|g" /etc/stunnel/stunnel.conf
      systemctl start openvpn@server-tls
      systemctl start stunnel4
    done
  }

  echo
  echo -e "${TEXT} MENU FAIL KONFIG OPENVPN                                   ${RESET}"
  echo
  echo -e "[01] ${GREEN}tcp${RESET}   - ${YELLOW}/etc/openvpn/server-tcp.conf${RESET}"
  echo -e "[02] ${GREEN}udp${RESET}   - ${YELLOW}/etc/openvpn/server-udp.conf${RESET}"
  echo -e "[03] ${GREEN}tls${RESET}   - ${YELLOW}/etc/openvpn/server-tls.conf${RESET}"
  echo -e "[00] ${GREEN}back${RESET}  - ${YELLOW}Exit from config file menu${RESET}"
  echo
  echo -e "${TEXT} ========================================================== ${RESET}"
  echo
  while true; do
    read -p "Pilih konfigurasi yang anda mahu edit: " _choice
    case "$_choice" in
    01 | tcp)
      server-tcp && break
      ;;
    02 | udp)
      server-udp && break
      ;;
    03 | tls)
      server-tls && break
      ;;
    00 | back)
      openvpnMenu && break
      ;;
    *)
      echo
      echo -e "${ERROR} INFO ${RESET}${TEXT} Dibatalkan ${RESET}"
      echo
      configurePkg
      ;;
    esac
  done
}

function detailPkg {
  local unit_id=$(systemctl show openvpn.service -p Id | cut -d '=' -f 2)
  local unit_name=$(systemctl show openvpn.service -p Names | cut -d '=' -f 2)
  local unit_desc=$(systemctl show openvpn.service -p Description | cut -d '=' -f 2)
  local is_active_tcp=$(systemctl is-active openvpn@server-tcp.service)
  local is_active_udp=$(systemctl is-active openvpn@server-udp.service)
  local is_active_tls=$(systemctl is-active openvpn@server-tls.service)
  local is_enable_tcp=$(systemctl is-enabled openvpn@server-tcp.service)
  local is_enable_udp=$(systemctl is-enabled openvpn@server-udp.service)
  local is_enable_tls=$(systemctl is-enabled openvpn@server-tls.service)

  local tcp_port=$(cat /etc/openvpn/server-tcp.conf | grep -w 'port' | cut -d ' ' -f 2)
  local udp_port=$(cat /etc/openvpn/server-udp.conf | grep -w 'port' | cut -d ' ' -f 2)
  local tls_port=$(cat /etc/openvpn/server-tls.conf | grep -w 'port' | cut -d ' ' -f 2)

  echo
  echo -e "${TEXT} ========================================================== ${RESET}"
  echo -e "${TEXT} Butiran perkhidmatan Openvpn                               ${RESET}"
  echo -e "${TEXT} ---------------------------------------------------------- ${RESET}"
  echo
  echo -e "${YELLOW}      Unit ID:${RESET} ${GREEN}$unit_id${RESET}"
  echo -e "${YELLOW}    Unit Name:${RESET} ${GREEN}$unit_name${RESET}"
  echo -e "${YELLOW}   Desciption:${RESET} ${GREEN}$unit_desc${RESET}"
  echo -e "${YELLOW} Status [TCP]:${RESET} ${GREEN}$is_active_tcp & $is_enable_tcp${RESET}"
  echo -e "${YELLOW} Status [UDP]:${RESET} ${GREEN}$is_active_udp & $is_enable_udp${RESET}"
  echo -e "${YELLOW} Status [TLS]:${RESET} ${GREEN}$is_active_tls & $is_enable_tls${RESET}"
  echo -e "${YELLOW}   Port lists:${RESET} ${GREEN}$tcp_port(tcp) $udp_port(udp) $tls_port(tls) ${RESET}"
  echo
  echo -e "${TEXT} ---------------------------------------------------------- ${RESET}"
  echo -e "${TEXT} Dicipta oleh Doctype, Dikuasakan oleh Cybertize.           ${RESET}"
  echo -e "${TEXT} ========================================================== ${RESET}"
  echo
}

function openvpnMenu {
  echo
  echo -e "${TEXT} ========================================================== ${RESET}"
  echo -e "${TEXT} MENU OPENVPN                                               ${RESET}"
  echo -e "${TEXT} ---------------------------------------------------------- ${RESET}"
  echo
  echo -e "[01] ${GREEN}reinstall${RESET}  - ${YELLOW}Reinstall openvpn package${RESET}"
  echo -e "[02] ${GREEN}uninstall${RESET}  - ${YELLOW}Uninstall openvpn package${RESET}"
  echo -e "[03] ${GREEN}configure${RESET}  - ${YELLOW}Edit/Configure openvpn config${RESET}"
  echo -e "[04] ${GREEN}detail   ${RESET}  - ${YELLOW}Show openvpn detail & status${RESET}"
  echo -e "[00] ${GREEN}quit     ${RESET}  - ${YELLOW}Exit from menu${RESET}"
  echo
  echo -e "${TEXT} ---------------------------------------------------------- ${RESET}"
  echo -e "${TEXT} Dicipta oleh Doctype, Dikuasakan oleh Cybertize.           ${RESET}"
  echo -e "${TEXT} ========================================================== ${RESET}"
  echo
  while true; do
    read -p "Masukkan pilihan anda: " _choice
    case "$_choice" in
    01 | reinstall)
      installPkg && break
      ;;
    02 | uninstall)
      uninstallPkg && break
      ;;
    03 | configure)
      configurePkg && break
      ;;
    04 | detail)
      detailPkg && break
      ;;
    00 | quit)
      exit 0
      ;;
    *)
      echo -e "${ERROR} RALAT ${RESET}${TEXT} Pilihan tidak sah ${RESET}"
      openvpnMenu
      ;;
    esac
  done
}

if [[ -d /etc/openvpn ]]; then
  openvpnMenu
elif [[ ! -d /etc/openvpn ]]; then
  installPkg
fi
